import { _decorator, Vec2 } from 'cc';
import DragTetris_CardFunctions from "../Common/DragTetris_CardFunctions";
import DragTetris_BlockGroup from "../Game/DragTetris_BlockGroup";
import DragTetris_Grid from "../Game/DragTetris_Grid";
import DragTetris_GameConfig from "./DragTetris_GameConfig";
import { DragTetris_GameStorage } from "./DragTetris_GameStorage";

export class DragTetris_GameData {
    static item_score: number = 10;

    //    /** 地形格子 XXL_Grid */
    static arrGrids: any[][] = [];
    //    /** 卡牌组 CardGroup */
    static arrCardGroups: any[] = [];
    static arrCardGroups_temp: DragTetris_BlockGroup[] = [];
    //    /** StartPos */
    static StartPos: Vec2;
    //    /** CurrentPos */
    static CurrentPos: Vec2;
    //    /** IsMouseDown  */
    static IsMouseDown: boolean = false;

    static InitData() {
        DragTetris_GameData.arrGrids = [];
        // DragTetris_GameData.arrCards = [];

        DragTetris_GameData.arrCardGroups = [];
        DragTetris_GameData.arrCardGroups_temp = [];
    }
    static UpdateScore() {
        if (DragTetris_GameData.GetScore() > DragTetris_GameStorage.data.high_score) {
            DragTetris_GameStorage.data.high_score = DragTetris_GameData.GetScore();

            DragTetris_GameStorage.saveData();
        }
    }
    static GetScore() {
        return DragTetris_GameStorage.data.map.score;
    }
    static SetScore(value) {
        DragTetris_GameStorage.data.map.score = value;
        DragTetris_GameStorage.saveData();
    }
    static ClearData() {
        // DragTetris_GameData.arrGrids = [];
        // DragTetris_GameData.arrCards = [];
        // DragTetris_GameData.arrCardGroups = [];
    }

    //    /** 保存数据 */
    static SaveData() {
        let serialized = [];

        for (let index = 0; index < DragTetris_GameData.arrCardGroups.length; index++) {
            const block: DragTetris_BlockGroup = DragTetris_GameData.arrCardGroups[index];
            let item = block.toJSON();
            serialized.push(item);
        }

        // console.log(serialized);
        DragTetris_GameStorage.data.map.arrCardGroups_temp = serialized;
        DragTetris_GameStorage.saveData();
    }
    //    /** 保存数据 */
    static SaveGridData() {
        let serialized = [];

        for (let x = 0; x < DragTetris_GameConfig.col; x++) {
            for (let y = 0; y < DragTetris_GameConfig.row; y++) {
                const scrGrid: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y);
                // console.log(`scrGrid`, scrGrid);

                if (scrGrid.block) {
                    // console.log(`scrGrid.block`, scrGrid.block);
                    let item = scrGrid.toJSON();
                    serialized.push(item);
                }
            }
        }

        DragTetris_GameStorage.data.map.blocks = serialized;
        DragTetris_GameStorage.saveData();
    }
}

window["DragTetris_GameData"] = DragTetris_GameData;